using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class rez : Feathertail.Command
    {
        private FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;

            if (!_Client.FeathertailState.ContainsKey(Helpers.CurrentDirectoryKey))
            {
                _Client.FeathertailState[Helpers.CurrentDirectoryKey] = "/";
            }
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length != 3)
            {
                return GetHelpText();
            }

            // Attempt to make a UUID object for comparing
            LLUUID uuid = null;

            try
            {
                uuid = new LLUUID(CmdLine[1]);
            }
            catch (Exception)
            {
            }

            // Find the object in question
            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

            InventoryItem itemOfInterest = null;

            iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.Name.Equals(CmdLine[1]) || item.ItemID.Equals(uuid))
                    {
                        itemOfInterest = item;
                        break;
                    }
                }
            }

            if (itemOfInterest == null)
            {
                return "Could not find Object: " + CmdLine[1];
            }

            // Check for sim coord modifier
            bool UseSimCoords = false;
            foreach (string s in CmdLine)
            {
                if (s.ToLower().Equals("/sim"))
                {
                    UseSimCoords = true;
                }
            }

            // Get X/Y/X
            string[] coords = CmdLine[2].Split(new char[] {',', ' '});
            if (coords.Length != 3)
            {
                return "Invalid coordinates specified" + Environment.NewLine + GetHelpText();
            }

            float x, y, z;
            try
            {
                if (!float.TryParse(coords[0], out x) || !float.TryParse(coords[1], out y) || !float.TryParse(coords[2], out z))
                {
                    return "Invalid coordinates specified" + Environment.NewLine + GetHelpText();
                }
            }
            catch (Exception)
            {
                return "Invalid coordinates specified" + Environment.NewLine + GetHelpText();
            }

            LLVector3 TargetPos = new LLVector3(_Client.Self.Position.X, _Client.Self.Position.Y, _Client.Self.Position.Z); // Need a copy constructor
            if (UseSimCoords)
            {
                TargetPos.X = x;
                TargetPos.Y = y;
                TargetPos.Z = z;
            }
            else
            {
                TargetPos.X += x;
                TargetPos.Y += y;
                TargetPos.Z += z;
            }

            // Attempt to rez it
            itemOfInterest.RezObject(TargetPos);
            
            return "Rez attempted.";
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "REZ" };
        }

        public string GetSummaryText()
        {
            return "Rez an object from inventory";
        }

        public string GetHelpText()
        {
            return "Rezzes an object at a point relative to your current position" + Environment.NewLine 
                 + "Usage: REZ [itemName | uuid] [x,y,z]" + Environment.NewLine 
                 + " Use an optional switch of /sim to specify sim coordinates";

        }

        #endregion
    }
}
